package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo4ToKafka {
  def main(args: Array[String]): Unit = {


    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //使用流模型
      .build()

    //创建flink sql的执行环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /**
      * 动态表：
      * 在流上定义表，
      * 可以往这个表中写数据，也可以读取这个表中的数据
      *
      */


    /**
      * 读取kafka 格式为json格式
      *
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE student (
        |  id STRING,
        |  name STRING,
        |  age BIGINT,
        |  gender STRING,
        |  clazz STRING
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'student_json',
        | 'properties.bootstrap.servers' = 'master:9092,node1:9092,node2:9092',
        | 'properties.group.id' = 'testGroup',
        | 'format' = 'json',
        | 'json.ignore-parse-errors' = 'true'
        |)
        |
      """.stripMargin)


    /**
      * sink 表
      *
      */
    bsTableEnv.executeSql(
      """
        |CREATE TABLE filter_student (
        |  id STRING,
        |  name STRING,
        |  age BIGINT,
        |  gender STRING,
        |  clazz STRING
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'filter_student',
        | 'properties.bootstrap.servers' = 'master:9092,node1:9092,node2:9092',
        | 'properties.group.id' = 'asdasd',
        | 'format' = 'json',
        | 'json.ignore-parse-errors' = 'true'
        |)
        |
      """.stripMargin)


    /**
      * 取出年龄大于23的学生，将解雇写道kafka中
      *
      */
    /*
        bsTableEnv.executeSql(
          """
            |insert into filter_student
            |select * from student
            |where age >= 23
            |
          """.stripMargin)
    */


    /**
      * 如果kafka作为sink 表不能写入更新的流， 只能写入append流
      *
      */


    //现在mysql中创建表,需要指定主键，字段名和字段类型都要匹配
    bsTableEnv.executeSql(
      """
        |CREATE TABLE gender_count (
        |  gender STRING,
        |  c BIGINT,
        |  PRIMARY KEY (gender) NOT ENFORCED
        |) WITH (
        | 'connector' = 'jdbc',
        | 'url' = 'jdbc:mysql://master:3306/bigdata?useUnicode=true&characterEncoding=utf-8',
        | 'table-name' = 'gender_count',
        | 'username' = 'root',
        | 'password' ='123456'
        |)
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |insert into gender_count
        |select gender,count(1)  as c from student group by gender
        |
      """.stripMargin)


  }

}
